package com.hawk.eye.client.logback.appender;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.filter.LevelFilter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import ch.qos.logback.core.Layout;
import ch.qos.logback.core.encoder.Encoder;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import com.fasterxml.jackson.core.type.TypeReference;
import com.hawk.eye.client.client.RequestParam;
import com.hawk.eye.client.toolkit.HawkEyeLogger;
import com.hawk.eye.client.toolkit.ServiceAuthHolder;
import com.hawk.eye.common.model.ServiceAuthData;
import com.hawk.eye.common.utils.JsonUtil;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: HawkEyeBaseAppender
 * @description: TODO 类描述
 * @author: salad
 * @date: 2025/2/15
 **/
public abstract class HawkEyeBaseAppender<E> extends AppenderBase<E> implements IHawkEyeAppender {

    private Encoder<E> encoder;

    @Override
    public void start() {
        Filter<E> filter = new Filter<E>() {
            @Override
            public FilterReply decide(E event) {
                return ((ILoggingEvent) event).getLevel().equals(Level.ERROR) ? FilterReply.ACCEPT : FilterReply.DENY;
            }
        };
        addFilter(filter);
        super.start();
    }

    @Override
    protected void append(E eventObject) {
        //不是健康的说明没有连接成功，或者授权失败，不进行告警推送
        if (!ServiceAuthHolder.isHealthy()) {
            return;
        }
        if (this.isStarted() && encoder != null) {
            if (eventObject instanceof ILoggingEvent) {
                ILoggingEvent loggingEvent = (ILoggingEvent) eventObject;
                if (Level.ERROR.equals(loggingEvent.getLevel())
                        || HawkEyeLogger.IS_SKIP_SEND_MARKER.equals(loggingEvent.getMarker())) {
                    byte[] encodedString = encoder.encode(eventObject);
                    String logMessage = new String(encodedString, StandardCharsets.UTF_8);
                    //请求参数
                    RequestParam requestParam = new RequestParam();
                    Map<String, Object> alarmLogAsMap = JsonUtil.parse(logMessage, new TypeReference<Map<String, Object>>() {
                    });
                    requestParam.setAlarmLogAsMap(alarmLogAsMap);
                    ServiceAuthData sad = ServiceAuthHolder.getAuthData();
                    requestParam.setServiceAuthData(sad);
                    send(requestParam);
                }
            }
        }
    }

    public void setLayout(Layout<E> layout) {
        LayoutWrappingEncoder<E> lwe = new LayoutWrappingEncoder<>();
        lwe.setLayout(layout);
        lwe.setContext(this.context);
        this.encoder = lwe;
    }

    public Encoder<E> getEncoder() {
        return encoder;
    }

    public void setEncoder(Encoder<E> encoder) {
        this.encoder = encoder;
    }

}
